package com.YHVision.qiuzhike.service.impl;

import com.YHVision.qiuzhike.common.ErrorCode;
import com.YHVision.qiuzhike.exception.BusinessException;
import com.YHVision.qiuzhike.model.entity.*;
import com.YHVision.qiuzhike.service.CompanyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.YHVision.qiuzhike.mapper.CompanyThumbMapper;
import com.YHVision.qiuzhike.service.CompanyThumbService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 企业点赞服务实现
 *
 * @author <a href="">YHVision-小y</a>
 * @from <a href="todo">爱编程学习圈</a>
 */
@Service
@Slf4j
public class CompanyThumbServiceImpl extends ServiceImpl<CompanyThumbMapper, CompanyThumb> implements CompanyThumbService {

    @Resource
    private CompanyService companyService;

    /**
     * 企业点赞
     *
     * @param companyId
     * @param loginUserId
     * @return
     */
    @Override
    public int doCompanyThumb(long companyId, User loginUserId) {
        //获取实体
        Company company = companyService.getById(companyId);
        if (company == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        //获取登录用户id
        Long userId = loginUserId.getId();
        //串行点赞
        CompanyThumbService companyThumbService =(CompanyThumbService) AopContext.currentProxy();
        synchronized (String.valueOf(userId).intern()){
            return companyThumbService.doQuestionThumbInner(companyId, userId);
        }
    }

    /**
     * 封装事务方法
     * 企业点赞具体操作（内部操作）
     *
     * @param userId
     * @param companyId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int doQuestionThumbInner(long userId, long companyId) {
        //数据校验
        if (companyId <= 0 || userId <= 0) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR," 题目id或用户id不存在");
        }
        CompanyThumb companyThumb = new CompanyThumb();
        companyThumb.setUserId(userId);
        companyThumb.setCompanyId(companyId);
        QueryWrapper<CompanyThumb> companyQueryWrapper = new QueryWrapper<>(companyThumb);
        CompanyThumb oleCompanyThumb = this.getOne(companyQueryWrapper);

        boolean result;
        //用户已点赞
        if (oleCompanyThumb != null) {
            result = this.remove(companyQueryWrapper);
            if (result){
                //点赞数-1
                result = companyService.update()
                        .eq("id",companyId)
                        .gt("thumbNum",0)
                        .setSql("thumbNum = thumbNum - 1")
                        .update();
                return result ? -1 : 0;
            }
            else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }
        //未点赞
        else {
            result = this.save(companyThumb);
            if(result){
                result = companyService.update()
                        .eq("id",companyId)
                        .setSql("thumbNum = thumbNum + 1")
                        .update();
                return result ? 1 : 0;
            }
            else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }
    }

}

